1
'*************************** Module Header ******************************'
2 ' Module Name: WinINet.vb
3 ' Project: VBWebBrowserWithProxy
4 ' Copyright (c) Microsoft Corporation.
6 ' This class is a simple .NET wrapper of wininet.dll. It contains 2 extern
7 ' methods (InternetSetOption and InternetQueryOption) of wininet.dll. This
8 ' class can be used to set proxy, disable proxy, backup internet options
9 ' and restore internet options.
11 ' This source is subject to the Microsoft Public License.
12 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 ' All other rights reserved.
15 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
16 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
17 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
18 '*************************************************************************'
20 Imports System
.Runtime
.InteropServices
22 Public NotInheritable Class WinINet
24 Private Shared _agent
As String = Process
.GetCurrentProcess().ProcessName
27 ''' Set the LAN connection proxy server for current process.
29 ''' <param name="proxyServer">
32 ''' <returns></returns>
33 Public Shared
Function SetConnectionProxy(ByVal isMachineSetting
As Boolean, ByVal proxyServer
As String) As Boolean
34 If isMachineSetting
Then
35 Return SetConnectionProxy(String.Empty
, proxyServer
)
37 Return SetConnectionProxy(_agent
, proxyServer
)
42 ''' Set the LAN connection proxy server.
44 ''' <param name="agentName">
45 ''' If agentName is null or empty, this function will set the Lan proxy for
46 ''' the machine, else for the current process.
48 ''' <param name="proxyServer">The Proxy Server.</param>
49 ''' <returns></returns>
50 Public Shared
Function SetConnectionProxy(ByVal agentName
As String, ByVal proxyServer
As String) As Boolean
51 Dim hInternet
As IntPtr
= IntPtr
.Zero
53 If Not String.IsNullOrEmpty(agentName
) Then
54 hInternet
= NativeMethods
.InternetOpen(agentName
, CInt(Fix(INTERNET_OPEN_TYPE
.INTERNET_OPEN_TYPE_DIRECT
)), Nothing, Nothing, 0)
57 Return SetConnectionProxyInternal(hInternet
, proxyServer
)
59 If hInternet
<> IntPtr
.Zero
Then
60 NativeMethods
.InternetCloseHandle(hInternet
)
66 ''' Set the proxy server for LAN connection.
68 Shared
Function SetConnectionProxyInternal(ByVal hInternet
As IntPtr
, ByVal proxyServer
As String) As Boolean
71 Dim Options(2) As INTERNET_PER_CONN_OPTION
74 Options(0) = New INTERNET_PER_CONN_OPTION()
75 Options(0).dwOption
= CInt(Fix(INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_FLAGS
))
76 Options(0).Value
.dwValue
= CInt(Fix(INTERNET_OPTION_PER_CONN_FLAGS
.PROXY_TYPE_PROXY
))
79 Options(1) = New INTERNET_PER_CONN_OPTION()
80 Options(1).dwOption
= CInt(Fix(INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_SERVER
))
81 Options(1).Value
.pszValue
= Marshal
.StringToHGlobalAnsi(proxyServer
)
84 Options(2) = New INTERNET_PER_CONN_OPTION()
85 Options(2).dwOption
= CInt(Fix(INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_BYPASS
))
86 Options(2).Value
.pszValue
= Marshal
.StringToHGlobalAnsi("local")
88 ' Allocate a block of memory of the options.
89 Dim buffer
As System
.IntPtr
= Marshal
.AllocCoTaskMem(Marshal
.SizeOf(Options(0)) + Marshal
.SizeOf(Options(1)) + Marshal
.SizeOf(Options(2)))
91 Dim current
As System
.IntPtr
= buffer
93 ' Marshal data from a managed object to an unmanaged block of memory.
94 For i
As Integer = 0 To Options
.Length
- 1
95 Marshal
.StructureToPtr(Options(i
), current
, False)
96 current
= CType(CInt(current
) + Marshal
.SizeOf(Options(i
)), System
.IntPtr
)
99 ' Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
100 Dim option_list
As New INTERNET_PER_CONN_OPTION_LIST()
102 ' Point to the allocated memory.
103 option_list
.pOptions
= buffer
105 ' Return the unmanaged size of an object in bytes.
106 option_list
.Size
= Marshal
.SizeOf(option_list
)
108 ' IntPtr.Zero means LAN connection.
109 option_list
.Connection
= IntPtr
.Zero
111 option_list
.OptionCount
= Options
.Length
112 option_list
.OptionError
= 0
113 Dim size
As Integer = Marshal
.SizeOf(option_list
)
115 ' Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance.
116 Dim intptrStruct
As IntPtr
= Marshal
.AllocCoTaskMem(size
)
118 ' Marshal data from a managed object to an unmanaged block of memory.
119 Marshal
.StructureToPtr(option_list
, intptrStruct
, True)
121 ' Set internet settings.
122 Dim bReturn
As Boolean = NativeMethods
.InternetSetOption( _
123 hInternet
, INTERNET_OPTION
.INTERNET_OPTION_PER_CONNECTION_OPTION
, intptrStruct
, size
)
125 ' Free the allocated memory.
126 Marshal
.FreeCoTaskMem(buffer
)
127 Marshal
.FreeCoTaskMem(intptrStruct
)
129 ' Throw an exception if this operation failed.
131 Throw
New ApplicationException(" Set Internet Option Failed!")
134 ' Notify the system that the registry settings have been changed and cause
135 ' the proxy data to be reread from the registry for a handle.
136 NativeMethods
.InternetSetOption(hInternet
, INTERNET_OPTION
.INTERNET_OPTION_SETTINGS_CHANGED
, IntPtr
.Zero
, 0)
138 NativeMethods
.InternetSetOption(hInternet
, INTERNET_OPTION
.INTERNET_OPTION_REFRESH
, IntPtr
.Zero
, 0)
144 ''' Get the current system options for LAN connection.
145 ''' Make sure free the memory after restoration.
147 Public Shared
Function GetSystemProxy() As INTERNET_PER_CONN_OPTION_LIST
149 ' Query following options.
150 Dim Options(2) As INTERNET_PER_CONN_OPTION
152 Options(0) = New INTERNET_PER_CONN_OPTION()
153 Options(0).dwOption
= CInt(Fix(INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_FLAGS
))
154 Options(1) = New INTERNET_PER_CONN_OPTION()
155 Options(1).dwOption
= CInt(Fix(INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_SERVER
))
156 Options(2) = New INTERNET_PER_CONN_OPTION()
157 Options(2).dwOption
= CInt(Fix(INTERNET_PER_CONN_OptionEnum
.INTERNET_PER_CONN_PROXY_BYPASS
))
159 ' Allocate a block of memory of the options.
160 Dim buffer
As System
.IntPtr
= Marshal
.AllocCoTaskMem(Marshal
.SizeOf(Options(0)) + Marshal
.SizeOf(Options(1)) + Marshal
.SizeOf(Options(2)))
162 Dim current
As System
.IntPtr
= CType(buffer
, System
.IntPtr
)
164 ' Marshal data from a managed object to an unmanaged block of memory.
165 For i
As Integer = 0 To Options
.Length
- 1
166 Marshal
.StructureToPtr(Options(i
), current
, False)
167 current
= CType(CInt(current
) + Marshal
.SizeOf(Options(i
)), System
.IntPtr
)
170 ' Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
171 Dim Request
As New INTERNET_PER_CONN_OPTION_LIST()
173 ' Point to the allocated memory.
174 Request
.pOptions
= buffer
176 Request
.Size
= Marshal
.SizeOf(Request
)
178 ' IntPtr.Zero means LAN connection.
179 Request
.Connection
= IntPtr
.Zero
181 Request
.OptionCount
= Options
.Length
182 Request
.OptionError
= 0
183 Dim size
As Integer = Marshal
.SizeOf(Request
)
185 ' Query system internet options.
186 Dim result
As Boolean = NativeMethods
.InternetQueryOption(IntPtr
.Zero
, INTERNET_OPTION
.INTERNET_OPTION_PER_CONNECTION_OPTION
, Request
, size
)
189 Throw
New ApplicationException("Get System Internet Option Failed! ")
196 ''' Restore to the system proxy settings.
198 Public Shared
Function RestoreSystemProxy() As Boolean
199 Return RestoreSystemProxy(_agent
)
203 ''' Restore to the system proxy settings.
205 Public Shared
Function RestoreSystemProxy(ByVal agentName
As String) As Boolean
206 If String.IsNullOrEmpty(agentName
) Then
207 Throw
New ArgumentNullException("Agent name cannot be null or empty!")
210 Dim hInternet
As IntPtr
= IntPtr
.Zero
212 If Not String.IsNullOrEmpty(agentName
) Then
213 hInternet
= NativeMethods
.InternetOpen(agentName
, CInt(Fix(INTERNET_OPEN_TYPE
.INTERNET_OPEN_TYPE_DIRECT
)), Nothing, Nothing, 0)
216 Return RestoreSystemProxyInternal(hInternet
)
218 If hInternet
<> IntPtr
.Zero
Then
219 NativeMethods
.InternetCloseHandle(hInternet
)
225 ''' Restore to the system proxy settings.
227 Shared
Function RestoreSystemProxyInternal(ByVal hInternet
As IntPtr
) As Boolean
228 Dim request
= GetSystemProxy()
230 Dim size
As Integer = Marshal
.SizeOf(request
)
233 Dim intptrStruct
As IntPtr
= Marshal
.AllocCoTaskMem(size
)
235 ' Convert structure to IntPtr
236 Marshal
.StructureToPtr(request
, intptrStruct
, True)
238 ' Set internet options.
239 Dim bReturn
As Boolean = NativeMethods
.InternetSetOption(hInternet
, INTERNET_OPTION
.INTERNET_OPTION_PER_CONNECTION_OPTION
, intptrStruct
, size
)
241 ' Free the allocated memory.
242 Marshal
.FreeCoTaskMem(request
.pOptions
)
243 Marshal
.FreeCoTaskMem(intptrStruct
)
246 Throw
New ApplicationException(" Set Internet Option Failed! ")
249 ' Notify the system that the registry settings have been changed and cause
250 ' the proxy data to be reread from the registry for a handle.
251 NativeMethods
.InternetSetOption(hInternet
, INTERNET_OPTION
.INTERNET_OPTION_SETTINGS_CHANGED
, IntPtr
.Zero
, 0)
253 NativeMethods
.InternetSetOption(hInternet
, INTERNET_OPTION
.INTERNET_OPTION_REFRESH
, IntPtr
.Zero
, 0)